package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     *
     * @param category
     * @param session
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody Category category, HttpSession session) {
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
       // Employee emp = (Employee) session.getAttribute("employee");
        //category.setCreateUser(emp.getId());
       // category.setUpdateUser(emp.getId());

        boolean save = categoryService.save(category);
        return save ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 分类分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R queryPage(int page, int pageSize) {
        //分页构造器
        IPage p = new Page(page, pageSize);

        IPage pg = categoryService.page(p);
        List records = pg.getRecords();
        long total = pg.getTotal();
        Map map = new HashMap();
        map.put("records", records);
        map.put("total", total);
        return R.success(map);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById(Long id) {
        R r = categoryService.deleteById(id);
        return r;

    }

    /**
     * 修改分类
     * @param category
     * @param session
     * @return
     */
    @PutMapping
    public R UpdateById(@RequestBody Category category,HttpSession session) {
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
       // Employee emp = (Employee) session.getAttribute("employee");
        //category.setCreateUser(emp.getId());
        //category.setUpdateUser(emp.getId());

        boolean b = categoryService.updateById(category);

        return b ? R.success("修改分类成功") : R.error("修改分类失败");
    }

    /**
     * 查询商品分类列表数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R list(Category category) {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(category.getType() != null, Category::getType, category.getType() );
        List<Category> list = categoryService.list(qw);
        //return list != null ? R.success(list) : R.error("查询失败");
        return  R.success(list);
    }
}
